/// 好友申请实体模型
/// 
/// 用于表示好友申请列表中的单个申请信息
class FriendRequestEntity {
  /// 申请ID
  final int? requestId;
  
  /// 发送者ID
  final int? senderId;
  
  /// 发送者用户名
  final String? senderName;
  
  /// 发送者头像URL
  final String? senderAvatar;
  
  /// 发送者性别
  final String? senderGender;
  
  /// 🔥 发送者手机号（用于融云IM匹配）
  final String? senderPhone;
  
  /// 接收者ID
  final int? receiverId;
  
  /// 申请消息
  final String? message;
  
  /// 申请状态（pending-待处理、accepted-已通过、rejected-已拒绝）
  final String? status;
  
  /// 创建时间
  final String? createdAt;

  FriendRequestEntity({
    this.requestId,
    this.senderId,
    this.senderName,
    this.senderAvatar,
    this.senderGender,
    this.senderPhone,
    this.receiverId,
    this.message,
    this.status,
    this.createdAt,
  });

  /// 从JSON创建好友申请实体
  factory FriendRequestEntity.fromJson(Map<String, dynamic> json) {
    // ✅ 处理 status 字段（可能是 int 或 String）
    String? statusValue;
    final rawStatus = json['status'];
    if (rawStatus != null) {
      if (rawStatus is int) {
        // 服务器返回的是数字：1=待处理，2=已同意，3=已拒绝
        switch (rawStatus) {
          case 1:
            statusValue = 'pending';
            break;
          case 2:
            statusValue = 'accepted';
            break;
          case 3:
            statusValue = 'rejected';
            break;
          default:
            statusValue = 'unknown';
        }
      } else {
        statusValue = rawStatus.toString();
      }
    }
    
    return FriendRequestEntity(
      requestId: json['request_id'] ?? json['requestId'] ?? json['id'],
      senderId: json['sender_id'] ?? json['senderId'] ?? json['userid'],
      // ✅ 服务器返回的字段是 username（不是 sender_name）
      senderName: json['username'] ?? json['sender_name'] ?? json['senderName'],
      // ✅ 服务器返回的字段是 userAvatarUrl（不是 sender_avatar）
      senderAvatar: json['userAvatarUrl'] ?? json['sender_avatar'] ?? json['senderAvatar'],
      // ✅ 服务器返回的字段是 userGender（不是 sender_gender）
      senderGender: json['userGender'] ?? json['sender_gender'] ?? json['senderGender'],
      // 🔥 新增：发送者手机号（用于融云IM匹配）
      senderPhone: json['userPhone'] ?? json['sender_phone'] ?? json['senderPhone'],
      receiverId: json['receiver_id'] ?? json['receiverId'],
      message: json['message'] as String?,
      status: statusValue,
      createdAt: json['created_at'] ?? json['createdAt'],
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'request_id': requestId,
      'sender_id': senderId,
      'sender_name': senderName,
      'sender_avatar': senderAvatar,
      'sender_gender': senderGender,
      'sender_phone': senderPhone,
      'receiver_id': receiverId,
      'message': message,
      'status': status,
      'created_at': createdAt,
    };
  }

  /// 获取显示名称
  String get displayName => senderName ?? '未知用户';
  
  /// 获取头像URL（带默认值）
  String get avatarUrl => senderAvatar ?? '';
  
  /// 是否是待处理状态（只有状态为 pending 或 1 时才是待处理）
  bool get isPending => status == 'pending' || status == '待处理' || status == '1';
  
  /// 是否已通过（兼容数字/英文/中文枚举）
  bool get isAccepted => status == 'accepted' || status == '已同意' || status == '2';
  
  /// 是否已拒绝（兼容数字/英文/中文枚举）
  bool get isRejected => status == 'rejected' || status == '已拒绝' || status == '3';
  
  /// 获取状态显示文本
  String get statusText {
    switch (status) {
      case 'pending':
      case '待处理':
      case '1':
        return '待处理';
      case 'accepted':
      case '已同意':
      case '2':
        return '已同意';
      case 'rejected':
      case '已拒绝':
      case '3':
        return '已拒绝';
      default:
        return status ?? '未知';
    }
  }

  @override
  String toString() {
    return 'FriendRequestEntity(requestId: $requestId, senderName: $senderName, status: $status)';
  }

  /// 创建一个复制体，允许覆盖部分字段
  FriendRequestEntity copyWith({
    int? requestId,
    int? senderId,
    String? senderName,
    String? senderAvatar,
    String? senderGender,
    String? senderPhone,
    int? receiverId,
    String? message,
    String? status,
    String? createdAt,
  }) {
    return FriendRequestEntity(
      requestId: requestId ?? this.requestId,
      senderId: senderId ?? this.senderId,
      senderName: senderName ?? this.senderName,
      senderAvatar: senderAvatar ?? this.senderAvatar,
      senderGender: senderGender ?? this.senderGender,
      senderPhone: senderPhone ?? this.senderPhone,
      receiverId: receiverId ?? this.receiverId,
      message: message ?? this.message,
      status: status ?? this.status,
      createdAt: createdAt ?? this.createdAt,
    );
  }
}
